<?php

/* File: Mysql.class.php
 * Author: Micheal Luthor
 * Email : micheal.the.ranidae@gmail.com
 * Version: 0.0.2
 * Modified At: 2013-09-28 00:11
 * */


require_once("Database.interface.php");

class Mysql implements Database
{
    private $_host;
    private $_user;
    private $_password;
    private $_port;
	
	private $_database;
	
    private $_connect;
    private $_result;

    private $_errno;
    private $_error;

    public function Connect ( $host, $user, $password, $port)
    {
        $this->_connect = mysql_connect( $host, $user, $password);
        
        if ( false != $this->_connect )
        {
            $this->_host        = $host;
            $this->_user        = $user;
            $this->_password    = $password;
            $this->_port        = $port;
        }
            
        return !( false == $this->_connect );
    }
    
    public function Set( $option, $value )
    {
        switch ( $option )
        {
        case 'db' : 
            mysql_query( 'USE '.$value ); 
            $this->_database = $value;
            break;
        case 'time zone' :
            mysql_query( "SET TIME_ZOME =  '$value'" );
            break;
        case 'char set' :
            mysql_query( "SET NAMES '$value'" );
            break;
        default :
            break;
        }
    }
    
    public function Close ( )
    {
        mysql_colse($this->_connect);
    }
    
    
    public function CreateDB( $db_name )
    {
    
    }
    
    public function DropDB( $db_name )
    {

    }
    
    public function CreateTable( $tb_name )
    {

    }
    
    public function TruncateTable ( $tb_name )
    {

    }

    public function DropTable ( $tb_name )
    {

    }

	/* Exec the query directly.
	 *
	 * @query: The sql query string you wanna run.
	 */
    public function Query( $query )
    {
        $this->_result = mysql_query($query);
        $this->_errno = mysql_errno();
        $this->_error = mysql_error(); 
    }
    
    public function Select( $table, $fields, $where )
    {
        $sql =  'Select '.implode(',', $fields).' '.
                'FROM '.$table.' '.
                'WHERE '.mysql_real_escape_string($where);

        $this->Query($sql);
    }

    public function Insert( $table, $fields, $values )
    {
        $sql =  'INSERT INTO '. mysql_real_escape_string($table).' '.
                '('.mysql_real_escape_string($fields).') '.
                'VALUES '.
                $values;

        $this->Query($sql);
    }

    public function Update ( $table, $fields, $values, $where )
    {
        $length = count($fields);
        for ( $i=0; $i<$length; $i++ )
        {
            $fields{$i} .= ('='.$values{$i});
        }

        $fields = implode(",", $fields);

        $sql =  'UPDATE '.$table.' '.
                'Set '.$fields.' '.
                'Where '.$where;

        $this->Query($sql);
        return $this->HasError();
    }

    public function Delete ( $table, $where )
    {
        $sql =  'DELETE FROM '.$table.' '.
                'WHERE '.$where;

        $this->Query($sql);
    }

    public function AffectedRows()
    {
        return mysql_affected_rows();
    }

    public function LastInsertID()
    {
        return mysql_insert_id();
    }

    public function HasError()
    {
        return (0 == $this->_errno) ? false : true;
    }

    public function Error()
    {
        return $this->_error;
    }

    public function Errno()
    {
        return $this->_errno;
    }

    public function RealEscapeString($value)
    {
        return mysql_real_escape_string($value);
    }

    public function Read()
    {
        return mysql_fetch_array($this->_result, MYSQL_ASSOC);
    }
  	
  	/* Get table list form current database.
  	 *
  	 * @Return: If everything is ok, then return the list. or false
  	 *          will be returned.
  	 */  
    public function ListTables()
    {
    	if (false === $result = mysql_query('SHOW TABLES FROM '.$this->_database) )
    	{
    		return false;
    	}
    	
    	$list = array();
    	
    	$table_nr = mysql_num_rows($result);
    	for ( $i=0; $i<$table_nr; $i++ )
    	{
    		$tmp = mysql_fetch_array($result, MYSQL_ASSOC);
    		array_push($list, $tmp{'Tables_in_ranidae'});
    	}
    	return $list;
    }
}

$db = new Mysql();
?>
